$(function () {
    console.log(authorities);

    $("#share-bf").on("click",function (){
        let url = window.location.href; /* 获取完整URL */
        alert(url)
    })

//    获取文章ID 生成文章内容
    var uid = $_GET['uid']
    console.log(uid);
    var createdBy;
    let author

    function get_content() {
        $.ajax({
            url: baseURL + '/webapi/works/searchId',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                id: uid,
            },
            async: false,//同步
            success: function (data) {
                if (data.code != 1) {
                    alert(data.msg, window.location.href = "login.html")
                }
                console.log(data);
                createdBy = data.data.createdBy
                author = data.data.createdBy
                if (data.data.type != 'imgtext') {
                    $(".content_main").html(data.data.content);
                } else {
                    let imgtext_content = content_img_text_symbol(data.data.content)
                    console.log(imgtext_content);
                    for (let i = 0; i < imgtext_content.length; i++) {
                        let true_img_url = getAliossPath("work/" + data.data.id, imgtext_content[i][0]);
                        $(".content_main").append(
                            '<img src="' + true_img_url + '"><p style="margin: 0 auto;text-align: center">' + imgtext_content[i][1] + '</p>'
                        );
                    }


                }

                if (data.data.type != 'news' && data.data.type != 'imgtext') {
                    if (data.data.tpic != "" && data.data.tpic != null) {
                        let img_arr = img_symbol(data.data.tpic);
                        //for i 循环
                        for (let i = 0; i < img_arr.length; i++) {
                            let img_url = getAliossPath("work/" + data.data.id, img_arr[i])
                            $(".content_main").append('<img src="' + img_url + '">');
                        }
                    }
                }

                $(".title").html(data.data.title);
                $(".label").text(del_symbol(data.data.label));
                $(".atime").html(time(data.data.createdTime));
                $(".Author").html(data.data.author).text(data.data.uname);
                if (data.data.author == null) {
                    $(".item_Author").text(data.data.uname);
                    $(".item_Author").attr('user_id', "" + data.data.updatedBy)
                    $(".Author").text(data.data.uname);
                } else {
                    $(".item_Author").html(data.data.author);
                    $(".Author").html(data.data.author);
                }
                if (data.data.type == 'news') {
                    $(".user_guanzhu").hide();
                }
                if (data.data.updatedBy == user_data.id) {
                    $(".user_guanzhu").hide();
                }
                $(".number_heart").text(data.data.likes);
                $(".number_start").text(data.data.recom);
                $("#comment").text(data.data.commentCount);
                $(".video_src_content").attr('src', '' + getAliossPath("work/" + data.data.id, data.data.content) + '');
                // $("#item_Author_head_img").html(data.data.headImg);
                $("#item_Author_head_img").append(
                    '<img class="user_link" user_id="' + data.data.updatedBy + '" src="' + getAliossPath("user/" + data.data.updatedBy, data.data.headImg) + '">\n'
                );
            }

        })
    }

    get_content();

    //用户信息显示
    if (user_data.name != undefined) {
        $("#user_main_id").text('' + user_data.name + '');
        $("#top_user_main_id").text('' + user_data.name + '');
        $("#top_main_user_img").attr('src', '' + getAliossPath("user/" + user_data.id, user_data.headimg) + '');
        $("#user_main_img").attr('src', '' + getAliossPath("user/" + user_data.id, user_data.headimg) + '');
        $(".user_head_img").attr('src', '' + getAliossPath("user/" + user_data.id, user_data.headimg) + '');
    }
    //相关推荐方法
    let channel = $.session.get('channel')
    if (channel == "") {
        channel = 1
    } else if (channel == null) {
        channel = 1
    }
    if (channel == 1) {
        function about_recommend_deful() {
            $.ajax({
                url: baseURL + '/webapi/works/searchRecommend',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    prop: 0,
                    rotation: 0,
                    pageNum: 1,
                    pageSize: 3,
                },
                async: false,//同步
                success: function (data) {
                    //for i 循环
                    for (let i = 0; i < 3; i++) {
                        $(".about_recommend").append(
                            '  <a type="' + data.data[i].type + '" uid="' + data.data[i].id + '" class="news-item clearfix link" >\n' +
                            '      <div class="row">\n' +
                            // '        <span class="col-xs-5">\n' +
                            // '          <img alt="" src="' + getPicUrl(getAliossPath("user/" + data.data[i].createdBy, data.data[i].tpic), 500, 0) + '">\n' +
                            // '        </span>\n' +
                            '        <span class="col-xs-12">\n' +
                            '          <div class="news-title link" type="' + data.data[i].type + '" uid="' + data.data[i].id + '">' + data.data[i].title + '</div>\n' +
                            '          <div class="news-info">\n' +
                            '          ' + data.data[i].commentCount + '评论\n' +
                            '          </div>\n' +
                            '        </span>\n' +
                            '      </div>\n' +
                            '  </a>\n'
                        );
                    }
                }

            })
        }

        about_recommend_deful()
    } else {
        function about_recommend() {
            $.ajax({
                url: baseURL + '/webapi/works/searchChannelsAllPage',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    prop: 0,
                    rotation: 0,
                    pageNum: 1,
                    pageSize: 3,
                    channel: channel,
                },
                async: false,//同步
                success: function (data) {
                    for (let i = 0; i < 3; i++) {
                        $(".about_recommend").append(
                            '  <a type="' + data.data[i].type + '" uid="' + data.data[i].id + '" class="news-item clearfix link" >\n' +
                            '      <div class="row">\n' +
                            // '        <span class="col-xs-5">\n' +
                            // '          <img alt="" src="' + getPicUrl(getAliossPath("work/" + data.data[i].id, data.data[i].tpic), 500, 0) + '">\n' +
                            // '        </span>\n' +
                            '        <span class="col-xs-12">\n' +
                            '          <div class="news-title link" type="' + data.data[i].type + '" uid="' + data.data[i].id + '">' + data.data[i].title + '</div>\n' +
                            '          <div class="news-info">\n' +
                            '          ' + data.data[i].commentCount + '评论\n' +
                            '          </div>\n' +
                            '        </span>\n' +
                            '      </div>\n' +
                            '  </a>\n'
                        );
                    }
                }
            })
        }

        about_recommend()
    }

    //侧栏用户文章推荐
    function user_item_recommend() {
        $.ajax({
            url: baseURL + '/webapi/works/searchUserAllPage',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                user: createdBy,
                pageNum: 1,
                pageSize: 3,
            },
            async: false,//同步
            success: function (data) {
                if (data.code != -1004) {
                    //for i 循环
                    if (data.data.length != null || data.data.length != "") {
                        let his_for_i_num_max_num
                        if (data.data.length <= 3) {
                            his_for_i_num_max_num = data.data.length
                        } else {
                            his_for_i_num_max_num = 3
                        }
                        for (let i = 0; i < his_for_i_num_max_num; i++) {
                            $(".related-list").append(
                                '<li class="related-list-item"  type="' + data.data[i].type + '" uid="' + data.data[i].id + '">\n' +
                                '<a class="link" type="' + data.data[i].type + '" uid="' + data.data[i].id + '">' + data.data[i].title + '</a>\n' +
                                '</li>'
                            )
                        }
                    }
                }
            }
        })
    }

    user_item_recommend();

    //24小时热闻
    function day_hot_news() {
        $.ajax({
            url: baseURL + '/webapi/works/searchRecommend',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                prop: 0,
                rotation: 0,
                pageNum: 1,
                pageSize: 10,
            },
            async: false,//同步
            success: function (data) {
                for (let i = 0; i < 10; i++) {
                    $(".day_hot_news").append(
                        '<a type="' + data.data[i].type + '" uid="' + data.data[i].id + '" class="news-item">\n' +
                        '    <div class="news-title link" type="' + data.data[i].type + '" uid="' + data.data[i].id + '">' + data.data[i].title + '</div>\n' +
                        '    <div class="news-info">\n' +
                        '        ' + data.data[i].commentCount + '评论\n' +
                        '    </div>\n' +
                        '</a>\n'
                    );
                }

            }
        })
    }

    day_hot_news()
    //评论部分
    let commentCount
    let fast_comment
    let com_id
    comment(uid, 0, 10)
    if (user_data.name != "" && user_data.name != null) {
        comment_Unreviewed_user(uid, 0)
    }

    function comment(wid, pid, pageNum) {
        $.ajax({
            url: baseURL + '/webapi/works/comment/searchPage',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                wid: wid,
                pid: pid,
                pageNum: pageNum,
                pageSize: 10,
            },
            async: false,//同步
            success: function (data) {
                fast_comment = data.data
                //第一次进入时生成的评论
                for (let i = 0; i < fast_comment.length; i++) {
                    let comment_data = fast_comment[i]
                    commentCount = comment_data.commentCount
                    com_id = 'com_id' + comment_data.id
                    $(".comment-list").append(
                        '                        <div class="comment-item">\n' +
                        '                            <a class="avatar" >\n' +
                        '                                <div>\n' +
                        '                                    <img alt=""\n' +
                        '                                         src="' + getAliossPath("user/" + data.data[i].updatedBy, data.data[i].headImg) + '">\n' +
                        '                                </div>\n' +
                        '                            </a>\n' +
                        '                            <div class="comment-detail">\n' +
                        '                                <div class="user-info">\n' +
                        '                                    <a class="name" >' + comment_data.uname + '</a>\n' +
                        '                                    <span class="create-time">' + time(comment_data.atime) + '</span>\n' +
                        '                                </div>\n' +
                        '                                <p class="content">' + comment_data.content + '</p>\n' +
                        '                                <div class="footer">\n' +
                        '                                    <span Reply_comment_id="' + comment_data.id + '" class="Reply_comment" style="cursor: pointer;color: #406599" data-toggle="modal" href="#replay_modal">回复  ⋅ </span>\n' +
                        '                                    <span com_num="' + comment_data.commentCount + '" class="reply-num down" style=""uid="' + comment_data.id + '" >' +
                        '                                       <span class="active-1 com_num" uid="' + comment_data.id + '"  >' + comment_data.commentCount + '条回复</span>' +
                        '                                    </span>\n' +
                        '                                    <span aria-hidden="true" class="glyphicon  glyphicon-menu-down"></span>\n' +
                        // '                                    <span class="report-user ">\n' +
                        // '                                    <span aria-hidden="true" class="glyphicon glyphicon-bell jubao_but"></span>\n' +
                        // '                                </span>\n' +
                        '                                </div>\n' +
                        '                                <div class="comment-reply-list" style="display: block">\n' +
                        '                                    <div id="com_content_id' + comment_data.id + '"  class="回复内容" style="display: none">\n' +
                        '\n' +
                        '                                    </div>\n' +
                        '                                </div>\n' +
                        '                            </div>\n' +
                        '                        </div>'
                    );

                    //该用户未被审核的评论
                    if (user_data.name != "" && user_data.name != null) {
                        comment_Unreviewed_user(uid, 0,)
                    }
                }
            }

        })
    }

    //评论的评论
    $(".comment-list").on('click', '.reply-num', function () {
        let text = $(this).attr("class");
        Reply_comment_id = $(this).attr('uid');
        console.log(Reply_comment_id);
        if (text) {
            if (text.lastIndexOf("down") !== -1) {
                $(this).removeClass("down");
                $(this).addClass("up");
                $(this).parent('div').siblings().find('.回复内容').show();
                // $(this).find(".回复内容").show();
                $(this).text("收起回复");
            } else {
                $(this).removeClass("up");
                $(this).addClass("down");
                $(this).parent('div').siblings().find('.回复内容').hide();
                let com_num = $(this).attr('com_num');
                console.log(com_num);
                $(this).text(com_num + '条回复');
            }
        }
        let pid = $(this).attr('uid');
        com_com(uid, pid, 10);
        com_com_unreviewed(uid, Reply_comment_id);
    });

    function com_com(wid, pid, pageNum) {
        $.ajax({
            url: baseURL + '/webapi/works/comment/searchPage',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                wid: wid,
                pid: pid,
                pageNum: pageNum,
                pageSize: 10,
            },
            async: false,//同步
            success: function (data) {
                $("#com_content_id" + pid).empty();
                for (let i = 0; i < data.data.length; i++) {
                    $("#com_content_id" + pid).append(
                        '                <div class="comment-item">\n' +
                        '                    <a  class="avatar">\n' +
                        '                        <div>\n' +
                        '                            <img src="' + getAliossPath("user/" + data.data[i].updatedBy, data.data[i].headImg) + '"\n' +
                        '                                 alt="">\n' +
                        '                        </div>\n' +
                        '                    </a>\n' +
                        '                    <div class="comment-detail">\n' +
                        '                        <div class="user-info">\n' +
                        '                            <a class="name" >' + data.data[i].uname + '</a>\n' +
                        '                            <span class="create-time">' + time(data.data[i].createdTime) + '</span>\n' +
                        '                        </div>\n' +
                        '                        <p class="content">' + data.data[i].content + '</p>\n' +
                        '                        <div class="footer">\n' +
                        // '                            <span class="reply-btn">回复</span>\n' +
                        '                            <span style="float: right" aria-hidden="true"\n' +
                        // '                                  class="glyphicon glyphicon-bell jubao_but"></span>\n' +
                        '                        </div>\n' +
                        '                        <div class="comment-reply-list"></div>\n' +
                        '                    </div>\n' +
                        '                </div>'
                    );
                }
            }

        })
    }

    //添加新评论方法
    $("#user_new_comment").click(function () {
        comment_main_new_comment()
    });

    function comment_main_new_comment() {
        let content = $("#comment_main_user_new_comment").val();
        $.ajax({
            url: baseURL + '/webapi/works/comment/addWorksComment',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                "works": uid,
                "pid": 0,
                "content": content,
                "nodeType": 0,
                "nodePath": 0,
                "nodeLevel": 0,
            },
            async: false,//同步
            success: function (data) {
                alert(data.msg)
                comment_Unreviewed_user(uid, 0,)
            },
            error: function () {
                alert("请先登录", window.location.href = "login.html")
            }

        })
    }

    //评论回复方法
    let Reply_comment_id
    $(".container").on('click', '.Reply_comment', function () {
        Reply_comment_id = $(this).attr('Reply_comment_id');
        console.log(Reply_comment_id);
    });
    $("#Comment_reply_button").click(function () {
        let Comment_reply_textarea_content = $("#Comment_reply_textarea").val();
        $.ajax({
            url: baseURL + '/webapi/works/comment/addWorksComment',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                "works": uid,
                "pid": Reply_comment_id,
                "content": Comment_reply_textarea_content,
                "nodeType": 0,
                "nodePath": 0,
                "nodeLevel": 0,
            },
            async: false,//同步
            success: function (data) {
                alert(data.msg)
                $(".com_num").click();
                com_com(uid, Reply_comment_id, 20)
                com_com_unreviewed(uid, Reply_comment_id)

            },
            error: function () {
                alert("请先登录", window.location.href = "login.html")
            }
        })
    });

    //未审核的子评论
    function com_com_unreviewed(wid, pid) {
        console.log(wid);
        console.log(pid);
        $.ajax({
            url: baseURL + '/webapi/works/comment/searchByWorksandUser',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                works: wid,
                pid: pid,
                // pageNum: pageNum,
                // pageSize: 10,
            },
            async: false,//同步
            success: function (data) {
                console.log(data);
                console.log(pid);
                for (let i = 0; i < data.data.length; i++) {
                    $("#com_content_id" + pid).append(
                        '                <div class="comment-item">\n' +
                        '                    <a  class="avatar">\n' +
                        '                        <div>\n' +
                        '                            <img src="' + getAliossPath("user/" + data.data[i].updatedBy, data.data[i].headImg) + '"\n' +
                        '                                 alt="">\n' +
                        '                        </div>\n' +
                        '                    </a>\n' +
                        '                    <div class="comment-detail">\n' +
                        '                        <div class="user-info">\n' +
                        '                            <a class="name" >' + data.data[i].uname + '</a>\n' +
                        '                            <span class="create-time">' + time(data.data[i].createdTime) + '</span>\n' +
                        '                        </div>\n' +
                        '                        <p class="content">' + data.data[i].content + '</p>\n' +
                        '                        <div class="footer">\n' +
                        // '                            <span class="reply-btn">回复</span>\n' +
                        '                            <span style="float: right" aria-hidden="true"\n' +
                        // '                                  class="glyphicon glyphicon-bell jubao_but"></span>\n' +
                        '                        </div>\n' +
                        '                        <div class="comment-reply-list"></div>\n' +
                        '                    </div>\n' +
                        '                </div>'
                    );
                }
            }

        })
    }

    //查用户刚刚评论的未被审核的评论
    function comment_Unreviewed_user(wid, pid) {
        $.ajax({
            url: baseURL + '/webapi/works/comment/searchByWorksandUser',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                works: uid,
                pid: pid,
            },
            async: false,//同步
            success: function (data) {
                for (let i = 0; i < data.data.length; i++) {
                    comment_data = data.data[i]
                    $(".comment-list").append(
                        '                        <div class="comment-item">\n' +
                        '                            <a class="avatar" >\n' +
                        '                                <div>\n' +
                        '                                    <img alt=""\n' +
                        '                                         src="' + getAliossPath("user/" + data.data[i].updatedBy, data.data[i].headImg) + '">\n' +
                        '                                </div>\n' +
                        '                            </a>\n' +
                        '                            <div class="comment-detail">\n' +
                        '                                <div class="user-info">\n' +
                        '                                    <a class="name" >' + comment_data.uname + '</a>\n' +
                        '                                    <span class="create-time">' + time(comment_data.atime) + '</span>\n' +
                        '                                </div>\n' +
                        '                                <p class="content">' + comment_data.content + '</p>\n' +
                        '                                <div class="footer">\n' +
                        '                                    <span Reply_comment_id="' + comment_data.id + '" class="Reply_comment" style="cursor: pointer;color: #406599" data-toggle="modal" href="#replay_modal">回复  ⋅ </span>\n' +
                        '                                    <span com_num="' + comment_data.commentCount + '" class="reply-num down" style=""uid="' + comment_data.id + '" >' +
                        '                                       <span class="active-1 com_num" uid="' + comment_data.id + '"  >' + comment_data.commentCount + '条回复</span>' +
                        '                                    </span>\n' +
                        '                                    <span aria-hidden="true" class="glyphicon  glyphicon-menu-down"></span>\n' +
                        // '                                    <span class="report-user ">\n' +
                        // '                                    <span aria-hidden="true" class="glyphicon glyphicon-bell jubao_but"></span>\n' +
                        // '                                </span>\n' +
                        '                                </div>\n' +
                        '                                <div class="comment-reply-list" style="display: block">\n' +
                        '                                    <div id="com_content_id' + comment_data.id + '"  class="回复内容" style="display: none">\n' +
                        '\n' +
                        '                                    </div>\n' +
                        '                                </div>\n' +
                        '                            </div>\n' +
                        '                        </div>'
                    );
                }
            }
        })
    }

    if (user_data.name != null && user_data.name != "") {
        //点赞状态
        function search_liked_info(id) {
            $.ajax({
                url: baseURL + '/webapi/userLikes/searchUserLikes',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    uesr: id,
                    works: uid,
                },
                async: false,//同步
                success: function (data) {
                    if (data.code == 1) {
                        $("#like_icon").addClass('glyphicon-heart');
                        $("#like_icon").removeClass('glyphicon-heart-empty');
                    }
                }
            })
        }

        search_liked_info(user_data.id)

        //关注状态
        function follow_info(id) {
            $.ajax({
                url: baseURL + '/webapi/userFollow/searchUserFollow',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    uesr: id,
                    followId: author,
                },
                async: false,//同步
                success: function (data) {
                    if (data.code == 1) {
                        $(".user_guanzhu").addClass('user-followed');
                        $(".user_guanzhu").removeClass('user-follow');
                        $(".user_guanzhu").find("span").text("已关注");
                    }
                }
            })
        }

        follow_info(user_data.id)

        //收藏状态
        function search_collect_info(id) {
            $.ajax({
                url: baseURL + '/webapi/userWorks/searchCollecWorks',
                type: 'post',
                dataType: 'json',
                headers: {'authorities': authorities},
                data: {
                    uesr: id,
                    works: uid,
                },
                async: false,//同步
                success: function (data) {
                    if (data.code == 1) {
                        $("#collect_icon").addClass('glyphicon-star');
                        $("#collect_icon").removeClass('glyphicon-star-empty');
                    }
                }
            })
        }

        search_collect_info(user_data.id)

    }

    //点赞方法
    function link_id(id) {
        $.ajax({
            url: baseURL + '/webapi/userLikes/addUserLikes',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                works: id,
            },
            async: false,//同步
            success: function (data) {
            },
            error: function () {
                alert("请先登录", window.location.href = "login.html")
            }

        })
    }

    $(".like-bf").click(function () {
        link_id(uid)
    });


    //收藏方法
    function collect_id(id) {
        $.ajax({
            url: baseURL + '/webapi/userWorks/addCollecWorks',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                works: id,
            },
            async: false,//同步
            success: function (data) {
            }

        })
    }

    $(".collect-bf").click(function () {
        collect_id(uid)
    });

    //关注用户方法
    function followId_author(author_id) {
        $.ajax({
            url: baseURL + '/webapi/userFollow/addUserFollow',
            type: 'post',
            dataType: 'json',
            headers: {'authorities': authorities},
            data: {
                followId: author_id,
            },
            async: false,//同步
            success: function (data) {
                if (data.code == 1) {
                    let text = $(this).attr("class");
                    if (text) {
                        if (text.lastIndexOf("user-followed") !== -1) {
                            $(this).removeClass("user-followed");
                            $(this).addClass("user-follow");
                            $(this).find("span").text("关注");
                        } else {
                            $(this).removeClass("user-follow");
                            $(this).addClass("user-followed");
                            $(this).find("span").text("已关注");
                        }
                    }
                } else {
                    alert(data.msg, location.reload())
                }
            },
            error: function () {
                alert("请先登录", window.location.href = "login.html")
            }
        })
    }

    $(".user-follow").click(function () {
        followId_author(author)
    });


    //消除某些符号
    function del_symbol(del_symbol) {
        let arr = (del_symbol || "").split(',');
        return arr[0]
    }

    //时间截取
    function time(time) {
        let arr = (time || "").split('T');
        return arr[0]
    }

    //图片分割
    function img_symbol(img_data) {
        let arr = (img_data || "").split(',');
        return arr
    }

    //图文content分割
    function content_img_text_symbol(content_data) {
        let content_data_img_text = (content_data || "").split('')
        var allarr = [];
        var text = content_data;
        let arr = text.split("|ymkjimg|")
        for (let i = 0; i < arr.length; i++) {
            let a = arr[i].split("|ymkjtext|");
            allarr.push(a);
        }
        return allarr
    }

})